package f.f0.i;

import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* compiled from: Http2Connection */
public final class g implements Closeable {
    public static final ExecutorService u;
    public final boolean a;
    public final d b;
    public final Map<Integer, p> c = new LinkedHashMap();
    public final String d;
    public int e;
    public int f;
    public boolean g;
    public final ScheduledExecutorService h;
    public final ExecutorService i;
    public final s j;
    public boolean k;
    public long l = 0;
    public long m;
    public t n = new t();
    public final t o;
    public boolean p;
    public final Socket q;
    public final q r;
    public final f s;
    public final Set<Integer> t;

    /* compiled from: Http2Connection */
    public static class c {
        public Socket a;
        public String b;
        public g.g c;
        public g.f d;
        public d e = d.a;
        public int f;

        public c(boolean z) {
        }
    }

    /* compiled from: Http2Connection */
    public static abstract class d {
        public static final d a = new a();

        /* compiled from: Http2Connection */
        public class a extends d {
            public void b(p pVar) throws IOException {
                pVar.c(b.REFUSED_STREAM);
            }
        }

        public void a(g gVar) {
        }

        public abstract void b(p pVar) throws IOException;
    }

    /* compiled from: Http2Connection */
    public class a extends f.f0.b {
        public final /* synthetic */ int b;
        public final /* synthetic */ b c;

        public a(String str, Object[] objArr, int i, b bVar) {
            this.b = i;
            this.c = bVar;
            super(str, objArr);
        }

        public void a() {
            try {
                g gVar = g.this;
                gVar.r.s(this.b, this.c);
            } catch (IOException unused) {
                g.a(g.this);
            }
        }
    }

    /* compiled from: Http2Connection */
    public class b extends f.f0.b {
        public final /* synthetic */ int b;
        public final /* synthetic */ long c;

        public b(String str, Object[] objArr, int i, long j) {
            this.b = i;
            this.c = j;
            super(str, objArr);
        }

        public void a() {
            try {
                g.this.r.t(this.b, this.c);
            } catch (IOException unused) {
                g.a(g.this);
            }
        }
    }

    /* compiled from: Http2Connection */
    public final class e extends f.f0.b {
        public final boolean b;
        public final int c;
        public final int d;

        public e(boolean z, int i, int i2) {
            super("OkHttp %s ping %08x%08x", r3.d, Integer.valueOf(i), Integer.valueOf(i2));
            this.b = z;
            this.c = i;
            this.d = i2;
        }

        /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x0025 */
        /* JADX WARNING: Can't wrap try/catch for region: R(7:0|(2:f|(3:7|8|23))|13|14|15|16|21) */
        /* JADX WARNING: Missing block: B:24:?, code skipped:
            return;
     */
        public void a() {
            /*
            r7 = this;
            r0 = f.f0.i.g.this;
            r1 = r7.b;
            r2 = r7.c;
            r3 = r7.d;
            java.util.Objects.requireNonNull(r0);
            r4 = f.f0.i.b.PROTOCOL_ERROR;
            if (r1 != 0) goto L_0x001f;
        L_0x000f:
            monitor-enter(r0);
            r5 = r0.k;	 Catch:{ all -> 0x001c }
            r6 = 1;
            r0.k = r6;	 Catch:{ all -> 0x001c }
            monitor-exit(r0);	 Catch:{ all -> 0x001c }
            if (r5 == 0) goto L_0x001f;
        L_0x0018:
            r0.k(r4, r4);	 Catch:{ IOException -> 0x0028 }
            goto L_0x0028;
        L_0x001c:
            r1 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x001c }
            throw r1;
        L_0x001f:
            r5 = r0.r;	 Catch:{ IOException -> 0x0025 }
            r5.r(r1, r2, r3);	 Catch:{ IOException -> 0x0025 }
            goto L_0x0028;
        L_0x0025:
            r0.k(r4, r4);	 Catch:{ IOException -> 0x0028 }
        L_0x0028:
            return;
            */
            throw new UnsupportedOperationException("Method not decompiled: f.f0.i.g$e.a():void");
        }
    }

    /* compiled from: Http2Connection */
    public class f extends f.f0.b implements f.f0.i.o.b {
        public final o b;

        public f(o oVar) {
            super("OkHttp %s", r3.d);
            this.b = oVar;
        }

        /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x001f */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing block: B:16:?, code skipped:
            r0 = f.f0.i.b.c;
     */
        /* JADX WARNING: Missing block: B:18:?, code skipped:
            r4.c.k(r0, r0);
     */
        /* JADX WARNING: Missing block: B:22:0x002c, code skipped:
            r2 = th;
     */
        public void a() {
            /*
            r4 = this;
            r0 = f.f0.i.b.INTERNAL_ERROR;
            r1 = r4.b;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r1.n(r4);	 Catch:{ IOException -> 0x001e, all -> 0x001b }
        L_0x0007:
            r1 = r4.b;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r2 = 0;
            r1 = r1.k(r2, r4);	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            if (r1 == 0) goto L_0x0011;
        L_0x0010:
            goto L_0x0007;
        L_0x0011:
            r1 = f.f0.i.b.NO_ERROR;	 Catch:{ IOException -> 0x001e, all -> 0x001b }
            r0 = f.f0.i.b.CANCEL;	 Catch:{ IOException -> 0x001f }
            r2 = f.f0.i.g.this;	 Catch:{ IOException -> 0x0026 }
            r2.k(r1, r0);	 Catch:{ IOException -> 0x0026 }
            goto L_0x0026;
        L_0x001b:
            r2 = move-exception;
            r1 = r0;
            goto L_0x002d;
        L_0x001e:
            r1 = r0;
        L_0x001f:
            r0 = f.f0.i.b.PROTOCOL_ERROR;	 Catch:{ all -> 0x002c }
            r1 = f.f0.i.g.this;	 Catch:{ IOException -> 0x0026 }
            r1.k(r0, r0);	 Catch:{ IOException -> 0x0026 }
        L_0x0026:
            r0 = r4.b;
            f.f0.c.f(r0);
            return;
        L_0x002c:
            r2 = move-exception;
        L_0x002d:
            r3 = f.f0.i.g.this;	 Catch:{ IOException -> 0x0032 }
            r3.k(r1, r0);	 Catch:{ IOException -> 0x0032 }
        L_0x0032:
            r0 = r4.b;
            f.f0.c.f(r0);
            throw r2;
            */
            throw new UnsupportedOperationException("Method not decompiled: f.f0.i.g$f.a():void");
        }
    }

    static {
        Class cls = g.class;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        byte[] bArr = f.f0.c.a;
        u = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, timeUnit, synchronousQueue, new f.f0.d("OkHttp Http2Connection", true));
    }

    public g(c cVar) {
        c cVar2 = cVar;
        t tVar = new t();
        this.o = tVar;
        this.p = false;
        this.t = new LinkedHashSet();
        this.j = s.a;
        this.a = true;
        this.b = cVar2.e;
        this.f = 1;
        this.f = 3;
        this.n.b(7, 16777216);
        this.d = cVar2.b;
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new f.f0.d(f.f0.c.n("OkHttp %s Writer", r6), false));
        this.h = scheduledThreadPoolExecutor;
        if (cVar2.f != 0) {
            long j = (long) cVar2.f;
            scheduledThreadPoolExecutor.scheduleAtFixedRate(new e(false, 0, 0), j, j, TimeUnit.MILLISECONDS);
        }
        this.i = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue(), new f.f0.d(f.f0.c.n("OkHttp %s Push Observer", r6), true));
        tVar.b(7, 65535);
        tVar.b(5, 16384);
        this.m = (long) tVar.a();
        this.q = cVar2.a;
        this.r = new q(cVar2.d, true);
        this.s = new f(new o(cVar2.c, true));
    }

    public static void a(g gVar) {
        Objects.requireNonNull(gVar);
        try {
            b bVar = b.PROTOCOL_ERROR;
            gVar.k(bVar, bVar);
        } catch (IOException unused) {
        }
    }

    public void close() throws IOException {
        k(b.NO_ERROR, b.CANCEL);
    }

    public void flush() throws IOException {
        this.r.flush();
    }

    public void h0(int i, long j) {
        try {
            this.h.execute(new b("OkHttp Window Update %s stream %d", new Object[]{this.d, Integer.valueOf(i)}, i, j));
        } catch (RejectedExecutionException unused) {
        }
    }

    public void k(b bVar, b bVar2) throws IOException {
        IOException e;
        p[] pVarArr = null;
        try {
            t(bVar);
            e = null;
        } catch (IOException e2) {
            e = e2;
        }
        synchronized (this) {
            if (!this.c.isEmpty()) {
                pVarArr = (p[]) this.c.values().toArray(new p[this.c.size()]);
                this.c.clear();
            }
        }
        if (pVarArr != null) {
            for (p c : pVarArr) {
                try {
                    c.c(bVar2);
                } catch (IOException e3) {
                    if (e != null) {
                        e = e3;
                    }
                }
            }
        }
        try {
            this.r.close();
        } catch (IOException e4) {
            if (e == null) {
                e = e4;
            }
        }
        try {
            this.q.close();
        } catch (IOException e5) {
            e = e5;
        }
        this.h.shutdown();
        this.i.shutdown();
        if (e != null) {
            throw e;
        }
    }

    public synchronized p n(int i) {
        return (p) this.c.get(Integer.valueOf(i));
    }

    public synchronized int o() {
        int i;
        t tVar = this.o;
        i = Integer.MAX_VALUE;
        if ((tVar.a & 16) != 0) {
            i = tVar.b[4];
        }
        return i;
    }

    public final synchronized void q(f.f0.b bVar) {
        synchronized (this) {
        }
        if (!this.g) {
            this.i.execute(bVar);
        }
    }

    public boolean r(int i) {
        return i != 0 && (i & 1) == 0;
    }

    public synchronized p s(int i) {
        p pVar;
        pVar = (p) this.c.remove(Integer.valueOf(i));
        notifyAll();
        return pVar;
    }

    public void t(b bVar) throws IOException {
        synchronized (this.r) {
            synchronized (this) {
                if (this.g) {
                    return;
                }
                this.g = true;
                int i = this.e;
                this.r.o(i, bVar, f.f0.c.a);
            }
        }
    }

    public synchronized void u(long j) {
        long j2 = this.l + j;
        this.l = j2;
        if (j2 >= ((long) (this.n.a() / 2))) {
            h0(0, this.l);
            this.l = 0;
        }
    }

    /* JADX WARNING: Missing exception handler attribute for start block: B:26:0x0056 */
    /* JADX WARNING: Can't wrap try/catch for region: R(3:26|27|28) */
    /* JADX WARNING: Missing block: B:16:?, code skipped:
            r3 = java.lang.Math.min((int) java.lang.Math.min(r12, r3), r8.r.d);
            r6 = (long) r3;
            r8.m -= r6;
     */
    /* JADX WARNING: Missing block: B:27:?, code skipped:
            java.lang.Thread.currentThread().interrupt();
     */
    /* JADX WARNING: Missing block: B:28:0x0062, code skipped:
            throw new java.io.InterruptedIOException();
     */
    public void v(int r9, boolean r10, g.e r11, long r12) throws java.io.IOException {
        /*
        r8 = this;
        r0 = 0;
        r1 = 0;
        r3 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r3 != 0) goto L_0x000d;
    L_0x0007:
        r12 = r8.r;
        r12.k(r10, r9, r11, r0);
        return;
    L_0x000d:
        r3 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r3 <= 0) goto L_0x0065;
    L_0x0011:
        monitor-enter(r8);
    L_0x0012:
        r3 = r8.m;	 Catch:{ InterruptedException -> 0x0056 }
        r5 = (r3 > r1 ? 1 : (r3 == r1 ? 0 : -1));
        if (r5 > 0) goto L_0x0030;
    L_0x0018:
        r3 = r8.c;	 Catch:{ InterruptedException -> 0x0056 }
        r4 = java.lang.Integer.valueOf(r9);	 Catch:{ InterruptedException -> 0x0056 }
        r3 = r3.containsKey(r4);	 Catch:{ InterruptedException -> 0x0056 }
        if (r3 == 0) goto L_0x0028;
    L_0x0024:
        r8.wait();	 Catch:{ InterruptedException -> 0x0056 }
        goto L_0x0012;
    L_0x0028:
        r9 = new java.io.IOException;	 Catch:{ InterruptedException -> 0x0056 }
        r10 = "stream closed";
        r9.<init>(r10);	 Catch:{ InterruptedException -> 0x0056 }
        throw r9;	 Catch:{ InterruptedException -> 0x0056 }
    L_0x0030:
        r3 = java.lang.Math.min(r12, r3);	 Catch:{ all -> 0x0054 }
        r4 = (int) r3;	 Catch:{ all -> 0x0054 }
        r3 = r8.r;	 Catch:{ all -> 0x0054 }
        r3 = r3.d;	 Catch:{ all -> 0x0054 }
        r3 = java.lang.Math.min(r4, r3);	 Catch:{ all -> 0x0054 }
        r4 = r8.m;	 Catch:{ all -> 0x0054 }
        r6 = (long) r3;	 Catch:{ all -> 0x0054 }
        r4 = r4 - r6;
        r8.m = r4;	 Catch:{ all -> 0x0054 }
        monitor-exit(r8);	 Catch:{ all -> 0x0054 }
        r12 = r12 - r6;
        r4 = r8.r;
        if (r10 == 0) goto L_0x004f;
    L_0x0049:
        r5 = (r12 > r1 ? 1 : (r12 == r1 ? 0 : -1));
        if (r5 != 0) goto L_0x004f;
    L_0x004d:
        r5 = 1;
        goto L_0x0050;
    L_0x004f:
        r5 = 0;
    L_0x0050:
        r4.k(r5, r9, r11, r3);
        goto L_0x000d;
    L_0x0054:
        r9 = move-exception;
        goto L_0x0063;
    L_0x0056:
        r9 = java.lang.Thread.currentThread();	 Catch:{ all -> 0x0054 }
        r9.interrupt();	 Catch:{ all -> 0x0054 }
        r9 = new java.io.InterruptedIOException;	 Catch:{ all -> 0x0054 }
        r9.<init>();	 Catch:{ all -> 0x0054 }
        throw r9;	 Catch:{ all -> 0x0054 }
    L_0x0063:
        monitor-exit(r8);	 Catch:{ all -> 0x0054 }
        throw r9;
    L_0x0065:
        return;
        */
        throw new UnsupportedOperationException("Method not decompiled: f.f0.i.g.v(int, boolean, g.e, long):void");
    }

    public void w(int i, b bVar) {
        try {
            this.h.execute(new a("OkHttp %s stream %d", new Object[]{this.d, Integer.valueOf(i)}, i, bVar));
        } catch (RejectedExecutionException unused) {
        }
    }
}
